<?php


namespace app\library\Points;

use app\library\Points\Triggers\ErrTrigger;
/**
 * 调用触发器工具
 */
class DoTrigger extends Db{
    protected function getKey($points_log){
        return $points_log['pid'] . '_' . $points_log['type'];
    }
    /**
     * 触发即时规则
     * @param type $points_log
     */
    function instant($points_log){
        $key = $this->getKey($points_log);
        $sql = "select * from points_rule where `key`='{$key}' and time=0";
        $rule = $this->queryOne($sql);
        
        $res = 1;
        if($rule){
            $trigger = $this->loadTrigger($rule['trigger'], $points_log);
            $result = $trigger->trigger($points_log);
            if(!$result){
                $res = "0".$trigger->errmsg;
            }
        }
        
        $data = [
            'trigger_result1' => $res
        ];
        
        $this->update('points_log', $data, ['id'=>$points_log['id']]);
        
        return null;
    }
    
    /**
     * 触发非即时规则, job
     */
    function noninstant(){
        $id = 0;
        $limit = 1000;
        $now = time();
        while(1){
            $sql = "select * from points_log where id > '{$id}' and `trigger_time` = 0 limit {$limit}";
            $points = $this->queryAll($sql);
            if( empty($points) ) break;
            
            foreach($points as $points_log){
                $id = $points_log['id'];
                
                $key = $this->getKey($points_log);
                $sql = "select * from points_rule where `key`='{$key}'";
                $rule = $this->queryOne($sql);
                
                if($rule){//规则存在
                    $time = $now - $points_log['create_time'];
                    if($time >= $rule['time']){//如果到了规则触发事件，则促发惠泽
                        $trigger = $this->loadTrigger($rule['trigger'], $points_log);
                        $result = $trigger->trigger($points_log);
                        if($result){
                            $res = 1;
                        }else{
                            $res = "0".$trigger->errmsg;
                        }
                        
                        $data = [
                            'trigger_time' => $now,
                            'trigger_result2' => $res
                        ];
                        $this->update('points_log', $data, ['id'=>$points_log['id']]);
                    }else{//如果没到规则触发事件，什么都不做
                        //do nothing
                    }
                }else{//规则不存在，直接设置为已经出发规则
                    $data = [
                        'trigger_time' => $now,
                        'trigger_result2' => 1
                    ];
                    $this->update('points_log', $data, ['id'=>$points_log['id']]);
                }
                
                
            }
        }
       
        return null;
    }
    
    protected function loadTrigger($trigger_name, $points_log){
        $class = $trigger_name.'Trigger';
        $file = __DIR__.DIRECTORY_SEPARATOR.'Triggers'.DIRECTORY_SEPARATOR.$class.'.php';
        if(!file_exists($file)){
            $trigger = ErrTrigger::getInstance();
            $trigger->setErr('触发器文件不存在');
        }else{
            try {
                if( !class_exists($class) ){
                    include $file;
                }
                $trigger = $class::getInstance();
            } catch (\Exception $ex) {
                $trigger = ErrTrigger::getInstance();
                $trigger->setErr('触发器报错：'.strval($ex));
            }
        }
        
        
        return $trigger;
    }
}
